const HID = (window as any).HID

import { sample, flatten } from 'underscore';
import web from './web'

export class Portas {
    static device: any = null
    deviceInfo: any
    constructor() {
        var devices = HID.devices();
        this.deviceInfo = devices.find(function (d: { vendorId: number; productId: number }) {
            return d.vendorId === 1267 && d.productId === 9377;
        });

    }
    open() {
        try {
            Portas.device = new HID.HID(this.deviceInfo.path);
        } catch (err) {
            console.log(err)
        }
    }
    close() {
        Portas.device.close()
    }
    write(data: Buffer) {
        Portas.device.write(data)
    }
}


export async function write(str: string) {
    await helperport(str)
}

export const hafi = () => {
    const zero = [[1, 1, 1, 1, 1]]
    const one = [
        [99, 1, 1, 1, 1],
        [1, 99, 1, 1, 1],
        [1, 1, 99, 1, 1],
        [1, 1, 1, 99, 1],
        [1, 1, 1, 1, 99]
    ]
    const two = [
        [99, 99, 1, 1, 1],
        [99, 1, 99, 1, 1],
        [99, 1, 1, 99, 1],
        [99, 1, 1, 1, 99],
        [1, 99, 99, 1, 1],
        [1, 99, 1, 99, 1],
        [1, 99, 1, 1, 99],
        [1, 1, 99, 99, 1],
        [1, 1, 99, 1, 99],
        [1, 1, 1, 99, 99],
    ]

    const three = [
        [99, 99, 99, 1, 1],
        [99, 99, 1, 99, 1],
        [99, 99, 1, 1, 99],
        [99, 1, 99, 99, 1],
        [99, 1, 99, 1, 99],
        [99, 1, 1, 99, 99],
        [1, 99, 99, 99, 1],
        [1, 99, 99, 1, 99],
        [1, 99, 1, 99, 99],
        [1, 1, 99, 99, 99],
    ]

    const four = one.map(v => {
        return v.map(e => e === 1 ? 99 : 1)
    })
    const five = [
        [99, 99, 99, 99, 99]
    ]

    return {
        zero: zero.map(v => v.join(',').concat(',z')),
        one: one.map(v => v.join(',').concat(',z')),
        two: two.map(v => v.join(',').concat(',z')),
        three: three.map(v => v.join(',').concat(',z')),
        four: four.map(v => v.join(',').concat(',z')),
        five: five.map(v => v.join(',').concat(',z')),
    }
}

export const dhafi = hafi()

const list: { [key: string]: string[] } = {
    '你好': sample(flatten(Object.values(dhafi)), 5),
    '动': sample(flatten(Object.values(dhafi)), 5),
    '开': [
        '99,99,99,99,99,50,z'
    ],
    石头: [
        '1,1,1,1,1,50,z'
    ],
    '剪刀': [
        '1,99,99,1,1,50,z'
    ],
    布: [
        '99,99,99,99,99,50,z'
    ],
    '拳': [
        '1,1,1,1,1,50,z'
    ],
    "帅": [
        '0,0,0,0,0,50,z',
        '0,99,99,0,0,50,z',
    ],
    "可爱": [
        '0,0,0,0,0,50,z',
        '0,99,99,0,0,50,z',
    ],
    "漂亮": [
        '0,0,0,0,0,50,z',
        '0,99,99,0,0,50,z',
    ],
    "数": [
        '0,99,0,0,0,50,z',
        '0,99,99,0,0,50,z',
        '0,99,99,99,0,50,z',
        '0,99,99,99,99,50,z',
        '99,99,99,99,99,50,z',
        '99,0,0,0,99,50,z',
        '99,99,99,0,0,50,z',
        '99,99,0,0,0,50,z',
        '0,60,0,0,0,50,z',
        '0,0,0,0,0,50,z',
    ],
    "左": [
        '50,50,50,50,50,1,z'
    ],
    "右": [
        '50,50,50,50,50,99,z'
    ]
}

export async function writenull(name?: string) {
    if (name) {
        for (let key in list) {
            if (name.includes(key)) {
                await helperport(list[key])
            }
        }
    }
}

export default async function helperport(str: string | string[]) {
    if (typeof str == 'string') {
        await web(str)
    } else {
        await web(str.join('-'), '', false)
    }
}
